package handler;

import java.util.Arrays;

public final class LineHandler extends PointHandler{

	final java.util.List<DeformationLine> baseLineList;
	private class BaseLine extends DeformationLine{
		final int startPoint;
		final int endPoint;
		
		BaseLine(final int startPoint, final int endPoint){
			this.startPoint = startPoint;
			this.endPoint = endPoint;
		}
		@Override
		public DeformationPoint getStartPoint() {
			return basePoints[startPoint];
		}

		@Override
		public DeformationPoint getEndPoint() {
			return basePoints[endPoint];
		}
		
	}
	final java.util.List<DeformationLine> movedLineList;
	private class MovedLine extends DeformationLine{
		final int startPoint;
		final int endPoint;
		MovedLine(final int startPoint, final int endPoint){
			this.startPoint = startPoint;
			this.endPoint = endPoint;
		}
		@Override
		public DeformationPoint getStartPoint() {
			return movedPoints[startPoint];
		}

		@Override
		public DeformationPoint getEndPoint() {
			return movedPoints[endPoint];
		}
		
	}
	private final int[][] lines;
	public LineHandler(DeformationPoint[] basePoints, int[][] lines) {
		super(basePoints);
		DeformationLine[] baseLines = new DeformationLine[lines.length];
		DeformationLine[] movedLines = new DeformationLine[lines.length];
		for (int i = 0; i < lines.length; i++) {
			baseLines[i] = new BaseLine(lines[i][0], lines[i][1]);
			movedLines[i] = new MovedLine(lines[i][0], lines[i][1]);
		}
		this.baseLineList = java.util.Collections.unmodifiableList(java.util.Arrays.asList(baseLines));
		this.movedLineList = java.util.Collections.unmodifiableList(java.util.Arrays.asList(movedLines));
		this.lines = lines;
	}
	
	public LineHandler(DeformationPoint[] basePoints, DeformationPoint[] movedPoints, int[][] lines) {
		super(basePoints, movedPoints);
		DeformationLine[] baseLines = new DeformationLine[lines.length];
		DeformationLine[] movedLines = new DeformationLine[lines.length];
		for (int i = 0; i < lines.length; i++) {
			baseLines[i] = new BaseLine(lines[i][0], lines[i][1]);
			movedLines[i] = new MovedLine(lines[i][0], lines[i][1]);
		}
		this.baseLineList = java.util.Collections.unmodifiableList(java.util.Arrays.asList(baseLines));
		this.movedLineList = java.util.Collections.unmodifiableList(java.util.Arrays.asList(movedLines));
		this.lines = lines;
	}
	
	public int numberOfLines() {
		return baseLineList.size();
	}

	public java.util.List<DeformationLine> getBaseLines(){
		return baseLineList;
	}
	
	public java.util.List<DeformationLine> getMovedLines(){
		return movedLineList;
	}

	public int[][] getLines() {
		int[][] lines = this.lines;
		int[][] newLines = new int[lines.length][];
		for (int i = 0; i < lines.length; i++) {
			newLines[i] = Arrays.copyOf(lines[i], lines[i].length);
		}
		return newLines;
	}

}
